﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sweepids.Models.Dao;
using Sweepids.Models.Object;

namespace Sweepids.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/

        public ActionResult Index()
        {
            var categoryDao = new CategoryDao();
            var listCategory = categoryDao.ReadAll();
            var itemDao = new ItemDao();
            var itemListFinal = new List<Item>();
            var itemList = itemDao.ReadAll();
            foreach (var item in itemList)
            {
                if (item.EndTime > DateTime.Now)
                {
                    itemListFinal.Add(item);
                }
            }
            var viewModel = new SearchViewModel();
            viewModel.categoryList = listCategory;
            viewModel.itemList = itemListFinal;
            return View(viewModel);
        }
        [HttpPost]
        public ActionResult SelectCategory(string category)
        {
            
            var viewModel = new SearchViewModel();
            var categoryDao = new CategoryDao();
            var categoryList = categoryDao.ReadAll();
            viewModel.categoryList = categoryList;
            var itemDao = new ItemDao();
            var itemListFinal = new List<Item>();
            var itemList = new List<Item>();
            if (category.Equals("Category"))
            {
                itemList = itemDao.ReadAll();
                foreach (var item in itemList)
                {
                    if (item.EndTime > DateTime.Now)
                    {
                        itemListFinal.Add(item);
                    }
                }
                viewModel.itemList = itemListFinal;
                return View("Index", viewModel);
            }
            var cat = categoryDao.ReadByName(category);
            var Id = cat.CatId;
            itemList = itemDao.ReadAllForCategory(Id);
            foreach (var item in itemList)
            {
                if (item.EndTime > DateTime.Now)
                {
                    itemListFinal.Add(item);
                }
            }
            viewModel.itemList = itemListFinal;
            return View("CategorySelectedPage", viewModel);
        }
        public class SearchViewModel
        {
            public List<Category> categoryList { get; set; }
            public List<Item> itemList { get; set; }
        }
    }
}
